package tmt.usercenter.web.domain;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.tmt.annotation.Select2OptionItem;
import com.tmt.annotation.Select2OptionValue;
import com.tmt.jpa.jpaservice.SyncUpdate;
import com.voodoodyne.jackson.jsog.JSOGGenerator;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * 用户角色
 * Created by Stark on 2017/3/8.
 */
@Getter
@Setter
//@EntityListeners({AuditingEntityListener.class})
@Entity
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
@JsonIdentityInfo(generator = JSOGGenerator.class)
public class TmtRole extends BaseCreator implements Serializable {

    /**
     * 角色名称
     */
    @Select2OptionItem
    @Column(nullable = false)
    private String name;

    /**
     * 系统内多角色识别编码
     */
    @Select2OptionItem(order = 1)
    @Column(nullable = false)
    private String code;

    /**
     * 角色的分组ID。
     */
    @Column
    private String groupId;


    /**
     * 角色有效性
     */
    @Select2OptionItem(order = 2)
    @Column(nullable = false, columnDefinition = "TINYINT", length = 1)
    private boolean enabled;

    /**
     * 备注
     */
    private String remark;

    /**
     * 是否是该客户系统的默认角色
     */
    private boolean defaultRole = false;

    /**
     * 权限所属的客户系统。
     */
    @Select2OptionValue("originSystem.id")
    @ManyToOne(cascade = {CascadeType.REFRESH}, fetch = FetchType.LAZY)
    @JoinColumn(name = "originSystem_id")
    private ClientSystem originSystem;

    @SyncUpdate
    @Select2OptionValue("roleAuthorities[].authority.id")
    @OneToMany(mappedBy = "role", cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH})
    private List<TmtRoleAuthority> roleAuthorities;


    @OneToMany(mappedBy = "role", cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH})
    private List<TmtUserRole> userRoles;

    public TmtRole() {
    }

    public TmtRole(Long id) {
        this.setId(id);
    }

}
